package com.example.exp_5;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.List;
import java.util.Map;

public class TeacherInfo extends AppCompatActivity {
    Button btnAdd,btnDelete,btnUpdate,btnSearch;
    EditText edNumber,edName,edSex,edSalary;
    ListView teacherInfo;
    TextView showNumber,showName,showSex,showSalary;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.teacher_info);
        btnAdd = (Button) findViewById(R.id.btn_add);
        btnDelete = (Button) findViewById(R.id.btn_delete);
        btnUpdate = (Button) findViewById(R.id.btn_update);
        btnSearch = (Button) findViewById(R.id.btn_search);
        edNumber = (EditText) findViewById(R.id.ed_number);
        edName = (EditText) findViewById(R.id.ed_name);
        edSex = (EditText) findViewById(R.id.ed_sex);
        edSalary = (EditText) findViewById(R.id.ed_salary);
        teacherInfo = (ListView) findViewById(R.id.teacher_info);
        showNumber = (TextView) findViewById(R.id.show_number);
        showName = (TextView) findViewById(R.id.show_name);
        showSex = (TextView) findViewById(R.id.show_sex);
        showSalary = (TextView) findViewById(R.id.show_salary);

        @SuppressLint("InflateParams") View view = LayoutInflater.from(this).inflate(R.layout.info_head,null);
        teacherInfo.addHeaderView(view);
        displayTeacherInfo(TeacherBpo.getAllTeachers(getApplicationContext()));

        teacherInfo.setOnItemClickListener((adapterView, view15, i, l) -> {
            TextView singleNumber = (TextView) view15.findViewById(R.id.single_number);
            TextView singleName = (TextView) view15.findViewById(R.id.single_name);
            TextView singleSex = (TextView) view15.findViewById(R.id.single_sex);
            TextView singleSalary = (TextView) view15.findViewById(R.id.single_salary);
            showNumber.setText(singleNumber.getText().toString());
            showName.setText(singleName.getText().toString());
            showSex.setText(singleSex.getText().toString());
            showSalary.setText(singleSalary.getText().toString());
        });
        btnAdd.setOnClickListener(view1 -> {
            try {
                Teacher teacher1 = new Teacher(edNumber.getText().toString(),edName.getText().toString(),edSex.getText().toString(),edSalary.getText().toString());
                TeacherBpo.insert(TeacherInfo.this, teacher1);
                displayTeacherInfo(TeacherBpo.getAllTeachers(getApplicationContext()));
                cleanEd();
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "添加失败",Toast.LENGTH_SHORT).show();
            }
        });
        btnDelete.setOnClickListener(view12 -> {
            try {
                String number = showNumber.getText().toString();
                TeacherBpo.delete(TeacherInfo.this,number);
                displayTeacherInfo(TeacherBpo.getAllTeachers(getApplicationContext()));
                cleanShow();
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "删除失败",Toast.LENGTH_SHORT).show();
            }
        });
        btnUpdate.setOnClickListener(view13 -> {
            try {
                Teacher teacher12 = new Teacher(edNumber.getText().toString(),edName.getText().toString(),edSex.getText().toString(),edSalary.getText().toString());
                Teacher oldTeacher = new Teacher(showNumber.getText().toString(),showName.getText().toString(),showSex.getText().toString(),showSalary.getText().toString());
                TeacherBpo.update(TeacherInfo.this, teacher12,oldTeacher);
                displayTeacherInfo(TeacherBpo.getAllTeachers(getApplicationContext()));
                cleanEd();
                cleanShow();
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "更新失败",Toast.LENGTH_SHORT).show();
            }
        });
        btnSearch.setOnClickListener(view14 -> {
            try {
                Teacher teacher13 = new Teacher(edNumber.getText().toString(),edName.getText().toString(),edSex.getText().toString(),edSalary.getText().toString());
                displayTeacherInfo(TeacherBpo.search(getApplicationContext(), teacher13));
                cleanShow();
                cleanEd();
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "查询失败",Toast.LENGTH_SHORT).show();
            }
        });
    }


    public void displayTeacherInfo(List<Map<String,String>> list) {
        //List<Map<String,String>> teacherList = TeacherBpo.getAllTeachers(this);
        SimpleAdapter simpleAdapter = new SimpleAdapter(this,list,R.layout.single_info,new String[]{"number","name","sex","salary"},new int[]{R.id.single_number,R.id.single_name,R.id.single_sex,R.id.single_salary});
        teacherInfo.setAdapter(simpleAdapter);
    }
    public void cleanShow() {
        showNumber.setText("");
        showName.setText("");
        showSex.setText("");
        showSalary.setText("");
    }
    public void cleanEd() {
        edNumber.setText("");
        edName.setText("");
        edSex.setText("");
        edSalary.setText("");
    }
}
